package com.mianshi;

import java.util.Stack;

public class wuba {
    public static void main(String[] args) {
        System.out.println(decodeString("2[abc]3[cd]ef"));
    }

    public static String decodeString (String str) {
        // write code here
        StringBuffer sb = new StringBuffer();
        Stack<Integer> stack = new Stack<>();
        Stack<StringBuffer> ansStack = new Stack<>();
        int num = 0;
        char[] arr = str.toCharArray();
        for(char item : arr){
            if(Character.isDigit(item)){
                num = num*10+item-'0';
            } else if(item=='['){
                ansStack.add(sb);
                stack.add(num);
                sb = new StringBuffer();
                num = 0;
            } else if(Character.isAlphabetic(item)){
                sb.append(item);
            }else{
                StringBuffer tempSB = ansStack.pop();
                int tmp = stack.pop();
                for(int i = 0; i < tmp; i++){
                    tempSB.append(sb);
                }
                sb = tempSB;
            }
        }
        return sb.toString();
    }
}
